package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Objects;

public class ForwardedServlet extends HttpServlet {
    private static final long serialVersionUID = 4078821702715723847L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应头的内容类型
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>转发器3结果</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("此页面为servlet-dispatcher3的目标转发页面<br>");
        out.print("转发器由ServletContext.getNamedDispatcher(java.lang.String)生成<br>");
        out.print("获取request参数<br>");
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()) {
            String element = names.nextElement();
            String val = req.getParameter(element);
            if (Objects.nonNull(val)) {
                out.print(element + "：" + val + "<br>");
            }
        }
        names = req.getAttributeNames();
        out.print("获取request的属性<br>");
        while (names.hasMoreElements()) {
            String element = names.nextElement();
            Object val = req.getAttribute(element);
            if (Objects.nonNull(val)) {
                out.print(element + "：" + val + "<br>");
            }
        }
        out.print("</html>");
    }
}
